/**
 * @class UI视图数据代理
 * @author DuskyHuang 圣子
 * @description 提供视图相关常量和数据管理。
*/

import { Proxy, decorators } from 'cfw';
import { UI } from 'notifications';
import {
    FONT_BIG, FONT_MID, FONT_SML, LBL_BROWN, LBL_CANT, LBL_CYAN, LBL_GRAY,
    LBL_MAGENTA, LBL_BLUE, LBL_ORANGE, LBL_PURPLE, LBL_ROSE, LBL_WHITE, LBL_YELLOW, INIT_LANG, StarLevel, StarNameType, DeepNameType
} from 'const';
import { LBL_GREEN } from 'const';
import { user } from './user-proxy';

const { singleton } = decorators;

@singleton
export class UIProxy extends Proxy {

    declare public static readonly instance: UIProxy;
    private constructor() { super(); }

    public get fullscreen(): boolean { return this._fullscreen; }                   // 是否进入全屏

    public get showCelNames(): boolean { return this._show_cel_names; }
    public get showConstNames(): boolean { return this._show_const_names; }         
    public get showNebularNames(): boolean { return this._show_nebular_names; }     
    public get showGalaxyNames(): boolean { return this._show_galaxy_names; }       
    public get showNotes(): boolean { return this._show_notes; }
    public get starNameLevel(): StarLevel { return this._star_name_level; }
    public get starNameType(): StarNameType { return this._star_name_type; }
    public get deepNameType(): DeepNameType { return this._deep_name_type; }

    public get language(): language { return this._language; }                      // i18n当前语言
    public get lensLocked(): boolean { return this._lens_locked; }                  // 是否锁定镜头
    public get showLensVel(): boolean { return this._show_lens_vel; }               // 是否显示镜头绝对速度
    public get showEscVel(): boolean { return this._show_esc_vel; }                 // 是否显示当前逃逸速度
    public get showCivLvl(): boolean { return this._show_civ_lvl; }                 // 是否显示宇宙文明等级

    public localizeLanguage(arg: language) { this._language = arg; }

    public getStateByName(name: string): boolean {
        switch (name) {
            case '锁定镜头': return this.lensLocked;
            case '镜头速度': return this.showLensVel;
            case '逃逸速度': return this.showEscVel;
            case '文明等级': return this.showCivLvl;

            case '天体名': return this.showCelNames;
            case '星座名': return this.showConstNames;
            case '星云名': return this.showNebularNames;
            case '星系名': return this.showGalaxyNames;
            case '标注名': return this.showNotes;
            default: return false;
        }
    }

    public static get use4KTex(): boolean { return false; }

    public static readonly DEEP_DIST: number = 1000;    // 深空视半径

    public static readonly WPS_CULLING_VALVE = 0.01;    // 世界尺度剪裁阈值 
    public static readonly FOV_CULLING_VALVE = 0.1;     // 视锥体剪裁阈值 

    public static readonly NameLabelAtt: attenuation    // 名称随距离淡化阈值
        = [0.02, 0.2, 5, 10];

    public static readonly IndicativeLineAtt: attenuation // 指示线随距离淡入淡出范围
        = [0.1, 0.2, 0.8, 1.5];

    public static readonly GroupLineStrength = 63 / 255;  // 群组线强度

    public static readonly LabelColor = {
        // NAME:new Color(54)
        DEFAULT: LBL_WHITE,     // 默认颜色
        SUN: LBL_ORANGE,        // 太阳颜色
        PLANET: LBL_GREEN,      // 行星名称颜色
        SATELLITE: LBL_GRAY,    // 卫星名称颜色
        DWARF: LBL_BROWN,       // 矮行星名称颜色
        MINOR: LBL_GRAY,        // 小行星名称颜色
        BELTS: LBL_BROWN,       // 小行星带名称颜色
        COMET: LBL_PURPLE,      // 彗星名称颜色
        STAR: LBL_CYAN,         // 恒星名称颜色
        STARS: LBL_WHITE,       // 星群名称颜色
        CONST: LBL_BLUE,        // 星座名称颜色
        CLUSTER: LBL_PURPLE,    // 星团名称颜色
        NEBULA: LBL_MAGENTA,    // 星云名称颜色
        GALAXY: LBL_CANT,       // 星系名称颜色
        COMPANION: LBL_GRAY,    // 伴星系名称颜色
        LY: LBL_YELLOW,         // 光年圈颜色
        AU: LBL_ROSE,           // 天文单位圈颜色
        MILKY: LBL_CYAN,        // 银河结构颜色
        AXIS: LBL_YELLOW,       // 天体转轴颜色
        DIAMETER: LBL_WHITE,    // 天体直径颜色
        BLACKHOLE: LBL_ROSE,    // 黑洞的名称颜色
    }

    public static readonly FontSize = {
        MAIN: FONT_BIG,                               // 主要大小
        DEFAULT: FONT_MID,                            // 默认大小
        SECONDARY: FONT_SML,                          // 次要大小
        TITLE: FONT_BIG,                              // 标题大小
        STARS: FONT_SML,                              // 恒星名称
        CLUSTER: FONT_SML,                            // 星团名称
        NEBULA: FONT_SML,                             // 星云名称
        GALAXY: FONT_MID,                             // 星系名称（主）
        DWARF: FONT_SML,                              // 星系名称（矮）
        COORD: FONT_SML,                              // 坐标提示
        EQUATOR: FONT_MID,                            // 赤道提示
        POLAR: FONT_BIG,                              // 极点提示
        RULER: FONT_SML,                              // 尺度提示
        CONST: FONT_BIG,                              // 星座名称（大）
        CONST_SML: FONT_MID,                          // 星座名称（小）
        CONST_SML_AREA_VALUE: 800,               // 星座名称大小区分阈值
        GROUP: FONT_MID,                              // 群集大小
    }

    protected init(): void {
        this.on(FULL_SCREEN, (arg: boolean) => this._fullscreen = arg, 99);
        this.on(Labels.CELESTIAL, (show: boolean) => this._show_cel_names = show);
        this.on(Labels.CONSTELLATION, (show: boolean) => this._show_const_names = show);
        this.on(Labels.NEBULAR, (show: boolean) => this._show_nebular_names = show);
        this.on(Labels.GALAXY, (show: boolean) => this._show_galaxy_names = show);
        this.on(Labels.NOTE, (show: boolean) => this._show_notes = show);
        this.on(Labels.STAR_NAME_LEVEL, (level: StarLevel) => this._star_name_level = level);
        this.on(Labels.STAR_NAME_TYPE, (type: StarNameType) => this._star_name_type = type);
        this.on(LANGUAGE, (arg: language) => {
            this._language = arg;
            user.updateLanguage(arg);
        });
        this.on(LENS_PARAMS, (params: LensPanelParams) => {
            const { lock, showLensVel, showEscVel, showCivLvl } = params;
            this._lens_locked = !!lock;
            this._show_lens_vel = !!showLensVel;
            this._show_esc_vel = !!showEscVel;
            this._show_civ_lvl = !!showCivLvl;
        }, 99);
    }

    private _fullscreen: boolean = false;
    private _show_cel_names: boolean = true;
    private _show_const_names: boolean = false;
    private _show_nebular_names: boolean = true;
    private _show_galaxy_names: boolean = true;
    private _show_notes: boolean = true;
    private _star_name_level: StarLevel = StarLevel.LEVEL_2; // LEVEL_2|3|4
    private _star_name_type: StarNameType = StarNameType.NAMED;
    private _deep_name_type: DeepNameType = DeepNameType.NAMED; // TO OPT (和恒星命名法一起做成单一按钮)

    private _language: language = INIT_LANG;
    private _lens_locked: boolean = false;
    private _show_lens_vel: boolean = true;
    private _show_esc_vel: boolean = true;
    private _show_civ_lvl: boolean = false;

    /** @deprecated */ public get showScopeCylinders(): boolean { return false; }
    /** @deprecated */ public get showIndicativeLines(): boolean { return false; }
    /** @deprecated */ public get showSubStructures(): boolean { return false; }
}

const { Labels, FULL_SCREEN, LENS_PARAMS, LANGUAGE } = UI;

export const ui = UIProxy.instance;